<?php
class signController extends Controller_Base
{
    public function indexAction()
    {
        $this->inAction();
    }
    
    public function inAction()
    {
        $this->logInJump();
        $this->setData('title', '登录');
        $this->setView('SignIn/signin.tpl');
    }
    
    public function outAction()
    {
        Model_User::signOut();
        $this->forward('http://'. HOST . '/sign/in');
    }
    
    public function doSignInAction()
    {
        if(Model_User::isSignIn()){
            $this->ajaxRet(-1, 'Already logged in');
            return;
        }
        $email    = $this->getPost('email', '');
        $password = $this->getPost('password', '');
        $code     = $this->getPost('code', '');
        if(!Model_AuthCode::verify($code)){
            $this->ajaxRet(-1, '验证码错误，请重新输入');
            return;
        }
        try {
            Model_User::signIn($email, $password);
            $this->ajaxRet(0, 'success');
        }catch (Eren_DB_Exception $e){
            $this->ajaxRet(-1, '系统异常，稍后重试');
            return;
        }catch (Exception $e){
            $this->ajaxRet($e->getCode(), $e->getMessage());
            return;
        }
    }
}